//
//  NetWorking.m
//  XHNShangChengMoBan
//
//  Created by 张铁军 on 16/8/13.
//  Copyright © 2016年 ping. All rights reserved.
//

#import "NetWorking.h"
#import "BaseViewController.h"
#import "iToast.h"
#define wanGuoTiSiStr @"网络错误,请稍候再试!"

@implementation NetWorking
+(void)imageShangChuan:(NSDictionary *)option imageDic:(NSDictionary *)imageDic anBlock:(void(^)(NSDictionary *dic,NSError *error))block{
    
    [APIManager sheZhiIP:IP];
    APIManager *manager = [APIManager sharedInstance];
    
    [manager postPath:@""
           parameters:option
              dataDic:imageDic
             dataType:@"image/jpeg"
              success:^(AFHTTPRequestOperation *operation, id responseObject) {
                  
                  NSDictionary *result = [TCUtils getDictionaryWithUTF8Data:responseObject];
                  NSDictionary *requestDic = [result objectForKey:RESULT];
                  
                  block(requestDic,nil);
              }
              failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                  //                  [[iToast makeText:wanGuoTiSiStr] show];
                  NSLog(@"Error: %@", error);
                  NSLog(@"operation: %@", operation);
                  block(nil, error);
                  
              }];
}

#pragma mark 可以设置ip接口

+(void)NEWjieKouQingQiu:(NSString *)ipStr ANDIP:(NSString*)newipSTr And:(NSDictionary *)option anBlock:(void(^)(NSDictionary *dic,NSError *error))block{
    [APIManager sheZhiIP:newipSTr];
    
    APIManager *manager = [APIManager sharedInstance];
    
    [manager postPath:ipStr
           parameters:option
              success:^(AFHTTPRequestOperation *operation, id responseObject) {
                  
                  //强制utf_8使用任何编码 (收集)
//                  NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
//                  NSString *retStr = [[NSString alloc] initWithData:responseObject encoding:enc];
//                  NSDictionary *result = [TCUtils dictionaryFromJson:retStr];
////                  if ([newipSTr isEqualToString:@"www.sky616.com:81"] ) {
//                      block(result,nil);
////                  }
//                  
//                  
////                   block(result,nil);
                  
                  
               NSDictionary *result = [TCUtils getDictionaryWithUTF8Data:responseObject];
                  
                  NSDictionary *requestDic = [result objectForKey:RESULT];
                  //返回code == 400 走自动登录
                  if ([[requestDic objectForKey:Code]intValue] == 400) {
                      NSLog(@"%@",[requestDic objectForKey:Response]);
                      BaseViewController *base =   [[BaseViewController alloc]init];
                      if ([base defaultLoadUserName]!= nil) {
                          
                              NSMutableDictionary *dlrequestDic = [[NSMutableDictionary alloc]init];
                              [dlrequestDic setObject:[[BaseViewController alloc]defaultLoadUserName] forKey:@"userName"];
                              [dlrequestDic setObject:[[BaseViewController alloc]defaultPassWord] forKey:@"password"];
                          
                              [NetWorking ziDongLongin:ipStr dengLu:dlrequestDic and:1 And:option and:newipSTr anBlock:^(NSDictionary *dic, NSError *error) {
                                  
                                  block(dic,nil);
                              }];
                      }
                  }else if([[requestDic objectForKey:Code]intValue] == 200){
                      block(requestDic,nil);
                  }else{//非400 提示错误信息
                      
                      NSString *str = @"";
                      if (![[requestDic objectForKey:Response] isEqual:[NSNull null]]) {
                          str = [requestDic objectForKey:Response];
//                          if (![ipStr isEqualToString:TS_yiChe]) {
                              [TCUtils showAlertViewWithTitle:@"提示" message:str delegate:nil cancel:@"确定" other:nil tag:0];
//                          }
                          
                      }
                      
                      block(requestDic,nil);
                  }
                  
              }
              failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                  
                  [[iToast makeText:wanGuoTiSiStr] show];
                  NSLog(@"Error: %@", error);
                  NSLog(@"operation: %@", operation);
                  block(nil, error);
              }];

}

//400 手机自动登录
+(void)ziDongLongin:(NSString *)ipStr dengLu:(NSDictionary*)dengLuOption and:(int)ShiFouJM And:(NSDictionary *)option and:(NSString *)qingQiuIp anBlock:(void(^)(NSDictionary *dic,NSError *error))block{
    
    [APIManager sheZhiIP:IP];
    
    APIManager *manager = [APIManager sharedInstance];
    
    [manager postPath:DL_dengLuMain
           parameters:dengLuOption
              success:^(AFHTTPRequestOperation *operation, id responseObject) {
                  
         NSDictionary *result = [TCUtils getDictionaryWithUTF8Data:responseObject];
        
        [self NEWjieKouQingQiu:ipStr ANDIP:qingQiuIp And:option anBlock:^(NSDictionary *dic, NSError *error) {
           
            block(dic,error);
        }];

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [[iToast makeText:wanGuoTiSiStr] show];
        block(nil,error);
    }];
   
    
}


-(NSString *)gb2312toutf8:(NSData *) data{
    
    NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
    NSString *retStr = [[NSString alloc] initWithData:data encoding:enc];
    
    return retStr;
}

@end
